﻿using System.ComponentModel;
using System.Windows;

namespace WindowsPhoneToolbox.Controls
{
    /// <summary>
    /// Interaction logic for AppPhoneControl.xaml
    /// </summary>
    public partial class AppPhoneControl
    {
        private static bool IsInDesignModeStatic
        {
            get
            {
                return
                    (bool)
                    DependencyPropertyDescriptor
                        .FromProperty(DesignerProperties.IsInDesignModeProperty,
                                      typeof(FrameworkElement)).Metadata.DefaultValue;
            }
        }

        public static readonly RoutedEvent ActionEvent =
            EventManager.RegisterRoutedEvent("ActionCommand", RoutingStrategy.Bubble,
                                             typeof(RoutedEventHandler), typeof(AppPhoneControl));

        public event RoutedEventHandler ActionCommand
        {
            add { AddHandler(ActionEvent, value); }
            remove { RemoveHandler(ActionEvent, value); }
        }

        public AppPhoneControl()
        {
            InitializeComponent();
            if (IsInDesignModeStatic)
            {
                TxtAppName.Text = "Test App";
                TxtAppGuid.Text = "Guid";
                BntApp.Content = "Name App";
            }
            else
            {
                BntApp.Visibility = Visibility.Collapsed;
            }
        }

        public static readonly DependencyProperty AppNameProperty =
            DependencyProperty.Register("AppName", typeof(string), typeof(AppPhoneControl),
                                        new PropertyMetadata(default(string), AppNamePropertyChangedCallback));

        private static void AppNamePropertyChangedCallback(DependencyObject dependencyObject,
                                                           DependencyPropertyChangedEventArgs
                                                               dependencyPropertyChangedEventArgs)
        {
            ((AppPhoneControl)dependencyObject).TxtAppName.Text = (string)dependencyPropertyChangedEventArgs.NewValue;
            ((AppPhoneControl)dependencyObject).BntApp.Content = dependencyPropertyChangedEventArgs.NewValue;
            ((AppPhoneControl)dependencyObject).ShowRenameBnt();
        }

        public string AppName
        {
            get { return (string)GetValue(AppNameProperty); }
            set { 
                SetValue(AppNameProperty, value);
                ShowRenameBnt();
            }
        }

        public static readonly DependencyProperty AliasNameProperty =
            DependencyProperty.Register("AliasName", typeof(string), typeof(AppPhoneControl),
                                        new PropertyMetadata(default(string), AliasNamePropertyChangedCallback));


        private static void AliasNamePropertyChangedCallback(DependencyObject dependencyObject,
                                                           DependencyPropertyChangedEventArgs
                                                               dependencyPropertyChangedEventArgs)
        {
            ((AppPhoneControl)dependencyObject).ShowRenameBnt();
        }

        public string AliasName
        {
            get { return (string)GetValue(AliasNameProperty); }
            set
            {
                SetValue(AliasNameProperty, value);
                ShowRenameBnt();
            }
        }

        private void ShowRenameBnt()
        {
            if (AppName == AppGuid && !string.IsNullOrEmpty(AppGuid) )
            {
                BntRename.Visibility = Visibility.Visible;
                if (!string.IsNullOrEmpty(AliasName))
                {
                    TxtAppName.Text = AliasName;
                }
            }
            else
            {
                BntRename.Visibility = Visibility.Collapsed;
            }
        }

        public static readonly DependencyProperty AppGuidProperty =
            DependencyProperty.Register("AppGuid", typeof(string), typeof(AppPhoneControl),
                                        new PropertyMetadata(default(string), AppGuidPropertyChangedCallback));

        private static void AppGuidPropertyChangedCallback(DependencyObject dependencyObject,
                                                           DependencyPropertyChangedEventArgs
                                                               dependencyPropertyChangedEventArgs)
        {
            ((AppPhoneControl)dependencyObject).TxtAppGuid.Text = (string)dependencyPropertyChangedEventArgs.NewValue;
            ((AppPhoneControl)dependencyObject).BntApp.Tag = dependencyPropertyChangedEventArgs.NewValue;
            ((AppPhoneControl)dependencyObject).ShowRenameBnt();
        }

        public string AppGuid
        {
            get { return (string)GetValue(AppGuidProperty); }
            set
            {
                SetValue(AppGuidProperty, value); 
                ShowRenameBnt();
            }
        }

        public static readonly DependencyProperty IsAppCoreProperty =
                DependencyProperty.Register("IsAppCore", typeof(bool), typeof(AppPhoneControl), new PropertyMetadata(default(bool), IsAppCorePropertyChangedCallback));

        private static void IsAppCorePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var value = (bool)dependencyPropertyChangedEventArgs.NewValue;
            if (value)
            {
                ((AppPhoneControl)dependencyObject).BntApp.Visibility = Visibility.Visible;
                ((AppPhoneControl)dependencyObject).StackAppinst.Visibility = Visibility.Collapsed;
            }
            else
            {
                ((AppPhoneControl)dependencyObject).BntApp.Visibility = Visibility.Collapsed;
                ((AppPhoneControl)dependencyObject).StackAppinst.Visibility = Visibility.Visible;
            }
        }

        public bool IsAppCore
        {
            get { return (bool)GetValue(IsAppCoreProperty); }
            set
            {
                SetValue(IsAppCoreProperty, value);
            }
        }

        private void BntCoreLaunchClick(object sender, RoutedEventArgs e)
        {
            var temp = ActionEvent;
            if (temp != null)
            {
                var arg = new AppPhoneEvent(AppPhoneAction.Launch, BntApp.Content.ToString(), BntApp.Tag.ToString(),string.Empty) { RoutedEvent = temp };
                RaiseEvent(arg);
            }
        }

        private void BntLaunchClick(object sender, RoutedEventArgs e)
        {
            var temp = ActionEvent;
            if (temp != null)
            {
                var arg = new AppPhoneEvent(AppPhoneAction.Launch, TxtAppName.Text, TxtAppGuid.Text,string.Empty) { RoutedEvent = temp };
                RaiseEvent(arg);
            }
        }

        private void BntRemoveClick(object sender, RoutedEventArgs e)
        {
            var temp = ActionEvent;
            if (temp != null)
            {
                var arg = new AppPhoneEvent(AppPhoneAction.Uninstall, TxtAppName.Text, TxtAppGuid.Text,string.Empty) { RoutedEvent = temp };
                RaiseEvent(arg);
            }
        }

        private void BntExplorerClick(object sender, RoutedEventArgs e)
        {
            var temp = ActionEvent;
            if (temp != null)
            {
                var arg = new AppPhoneEvent(AppPhoneAction.Explorer, TxtAppName.Text, TxtAppGuid.Text,string.Empty) { RoutedEvent = temp };
                RaiseEvent(arg);
            }
        }

        private void BntRenameClick(object sender, RoutedEventArgs e)
        {
            var temp = ActionEvent;
            if (temp != null)
            {
                var arg = new AppPhoneEvent(AppPhoneAction.RenameCache, TxtAppName.Text, TxtAppGuid.Text,string.Empty) { RoutedEvent = temp };
                RaiseEvent(arg);
            }
        }
    }
}